package com.atguigu.cloud.controller;

import cn.hutool.core.date.DateUtil;
import com.atguigu.cloud.apis.PayFeignApi;
import com.atguigu.cloud.entities.PayDTO;
import com.atguigu.cloud.resp.ResultData;
import com.atguigu.cloud.resp.ReturnCodeEnum;
import jakarta.annotation.Resource;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @版本： V5.1.0
 * @版权： Copyright @ 2025 广东广昱信息科技有限公司版权所有
 * @作者： 广东广昱信息科技有限公司
 * @日期： 2025-07-23 9:32
 */
@RestController
public class OrderController {

    @Resource
    private PayFeignApi payFeignApi;

    @PostMapping("/feign/pay/add")
    public ResultData addOerder(@RequestBody PayDTO payDto){
        System.out.println("第一步：模拟本地addOrder新增订单成功(省略sql操作),第二步:再开启addPay支付微服务远程调用");
        ResultData resultData = payFeignApi.addPay(payDto);
        return resultData;
    }

    @GetMapping("/feign/pay/get/{id}")
    public ResultData getPayInfo(@PathVariable("id") Integer id){
        System.out.println("-----------支付微服务远程调用，按照id查询订单支付流水信息");
        ResultData resultData = null;
        try {
            System.out.println("调用开始------:"+ DateUtil.now());
            resultData =payFeignApi.getPayInfo(id);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("调用结束------:"+ DateUtil.now());
            resultData = ResultData.fail(ReturnCodeEnum.RC500.getCode(), e.getMessage());
        }
        return resultData;
    }

    @PutMapping("/feign/pay/update")
    public ResultData updateOrder(@RequestBody PayDTO payDto){
        System.out.println("-----------支付微服务远程调用，更新订单支付流水信息");
        ResultData resultData = payFeignApi.updatePay(payDto);
        return resultData;
    }

    @DeleteMapping("/feign/pay/del/{id}")
    public ResultData deletePayInfo(@PathVariable("id") Integer id){
        System.out.println("-----------支付微服务远程调用，删除订单支付流水信息");
        ResultData resultData = payFeignApi.deletePay(id);
        return resultData;
    }

    /**
     *
     * @return
     */
    @GetMapping("/feign/pay/mylb")
    public String mylb(){
        System.out.println("-----------支付微服务远程调用，查询支付微服务信息");
        return payFeignApi.mylb();
    }

}
